package handler

import (
	"github.com/gin-gonic/gin"
	"log"
	"task00/pkg/db"
	"task00/pkg/domain/account"
)

func (h *Handler) Registration(c *gin.Context) {
	var acc account.Registration
	if err := c.BindJSON(&acc); err != nil {
		c.JSON(400, gin.H{"error bind Registration account": err.Error()})
		log.Printf("error bind Registration account %v", err.Error())
		return
	}
	userId, _ := c.Cookie("id")
	result, err := h.service.Registration(c.Request.Context(), userId, acc)
	if err != nil {
		switch err.Error() {
		case db.ErrValidate.Error():
			c.JSON(400, gin.H{"error bind Registration account": err.Error()})
			log.Printf("error bind Registration account %v", err.Error())
			break
		case db.ErrAuthorize.Error():
			c.JSON(403, gin.H{"Already Authorized": err.Error()})
			log.Println("Already Auth on Register")
			break
		case db.ErrDuplicate.Error():
			c.JSON(409, "Email already exist")
			log.Printf("Register email failed %v", err.Error())
			break
		default:
			c.JSON(500, gin.H{"error bind Registration account": err.Error()})
			log.Printf("error service Registration account %v", err.Error())
		}
		return
	}

	c.JSON(201, result)
	log.Printf("Success registration: %v", result)
}

func (h *Handler) Login(c *gin.Context) {
	var acc account.Login
	if err := c.BindJSON(&acc); err != nil {
		c.IndentedJSON(401, gin.H{"error": "Invalid request"})
		return
	}

	id, err := h.service.Login(c.Request.Context(), acc)
	if err != nil {
		switch err.Error() {
		case db.ErrNotExist.Error():
			c.IndentedJSON(401, gin.H{"error": "Account does not exist"})
			log.Printf("Account does not exist")
			break
		default:
			c.IndentedJSON(500, gin.H{"error": err.Error()})
			log.Printf("error service Login error %v", err.Error())
		}
		return
	}

	c.IndentedJSON(200, gin.H{"id": id})
}

func (h *Handler) PutAccount(c *gin.Context) {

}

func (h *Handler) GetAccount(c *gin.Context) {
	id := c.Param("accountId")
	userId, _ := c.Cookie("id")
	result, err := h.service.GetAccount(c.Request.Context(), userId, id)
	if err != nil {
		switch err.Error() {
		case db.ErrParamNotFound.Error():
			c.JSON(400, gin.H{"message": "params id can't be empty"})
			log.Println("params id can't be empty on GetAccount call")
			break
		case db.ErrAuthorize.Error():
			c.JSON(401, gin.H{"error": err.Error()})
			log.Println(err.Error())
			break
		case db.ErrNotExist.Error():
			c.JSON(404, gin.H{"error": err.Error()})
			log.Printf("id:%v\terror:%v", id, err)
			break
		default:
			c.JSON(500, gin.H{"error": err.Error()})
			log.Println(err.Error())
		}
		return
	}

	c.JSON(200, result)
	log.Printf("Success GetAccount %v", result)
	return
}

func (h *Handler) DeleteAccount(c *gin.Context) {
	id := c.Param("accountId")
	userId, _ := c.Cookie("id")
	err := h.service.DeleteAccount(c.Request.Context(), userId, id)
	if err != nil {
		switch err.Error() {
		case db.ErrParamNotFound.Error():
			c.JSON(400, gin.H{"error": "problems with param"})
			log.Println("problems with param on DeleteAccount call")
			break
		case db.ErrAuthorize.Error():
			c.JSON(401, gin.H{"error": err.Error()})
			log.Println(err.Error())
			break
		case db.ErrDeleteFailed.Error():
			c.JSON(403, gin.H{"error": err.Error()})
			log.Println(err.Error())
			break
		default:
			c.JSON(500, gin.H{"error": err.Error()})
			log.Println(err.Error())
		}
		return
	}
	c.JSON(200, gin.H{})
	log.Printf("Success DeleteAccount %v", id)
	return
}
